import time
import json

import requests
import uiautomation as auto
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
cookies=''
chrome_driver=webdriver.Chrome()
chrome_driver.maximize_window()

chrome_driver.get('https://www.4399.com/')


def login_with_single_id():
    WebDriverWait(chrome_driver, 5).until(EC.element_to_be_clickable((By.ID, "login_tologin")))


login_control = chrome_driver.find_element(By.ID, "login_tologin")
login_control.click()
chrome_driver.add_argument('--headless')
chrome_driver.add_argument('blink-settings=imagesEnabled=false')
chrome_driver.switch_to.frame('popup_login_frame')
id_control=chrome_driver.find_element(By.ID,'username')
id_control.send_keys('3178071914')
pwd_control=chrome_driver.find_element(By.ID,'j-password')
pwd_control.send_keys('zzh20040608')
denglu=chrome_driver.find_element(By.XPATH,'//*[@id="login_form"]/fieldset/div[5]/input')
denglu.click()
time.sleep(3)

cookies = chrome_driver.get_cookies()
with open("cookies.json", 'w') as cookiefile:
    json.dump(cookies, cookiefile)

with open('cookies.json', 'r', encoding='utf-8') as cookiefile2:
    cookies = json.loads(cookiefile2.read())
    cookiestr = [item['name'] + '=' + item['value'] for item in cookies]
    print(cookiestr)
url='https://u.4399.com/profile/'
headers={
    'cookie':cookiestr,
    'user-agent':'Mozilla/5.0(Windows NT 10.0;Win64;x64) AppleWebKit/537.36(KHTML,like Gecko) Chrome/77.0.3865.90 Safari/537.36'
}
html=requests.get(url=url,headers=headers)
print(html.text)
